<template>
  <div class="binding-page password-page">
    <div class="setting-page-right-title">
      <div class="setting-right-name">
        <div class="setting-top-name">修改密码</div>
        <div class="setting-bottom-line"></div>
      </div>
    </div>
    <div class="binding-content">
      <el-form label-width="120px" label-position="right">
        <el-form-item label="当前登录账号">
          <span class="text-phone">{{phone}}</span>
        </el-form-item>
        <el-form-item label="旧密码">
          <el-input
            class="theme-input"
            placeholder="请输入旧密码"
            show-password
            v-model="params.password"
          >
            <img slot="prefix" src="../../assets/img/register/lock.png" height="25px">
          </el-input>
        </el-form-item>
        <el-form-item label="短信验证码">
          <el-input class="theme-input" placeholder="请输入短信验证码" v-model="params.yards">
            <img slot="prefix" src="../../assets/img/register/123.png" height="30px">
            <el-button
              type="text"
              slot="suffix"
              class="text-button-blue"
              @click="sendSmsPassword"
              :disabled="codeLeftTime!=60"
            >
              发送验证码
              <span v-if="codeLeftTime!=60">({{codeLeftTime}})</span>
            </el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input
            class="theme-input"
            placeholder="请输入新密码"
            show-password
            v-model="params.newPassword"
          >
            <img slot="prefix" src="../../assets/img/register/lock.png" height="25px">
          </el-input>
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input
            class="theme-input"
            placeholder="请输入新密码"
            show-password
            v-model="params.submitPass"
          >
            <img slot="prefix" src="../../assets/img/register/lock.png" height="25px">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="submit-button" @click="submit">确认修改密码</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      codeLeftTime: 60, //获取验证码间隔
      params: {
        phone: '',
        talentId: 3,
        password: '',
        yards: '',
        newPassword: '',
        submitPass: ''
      },
      phone: '',
      TalentInfo: {}
    }
  },
  computed: {},
  methods: {
    check() {
      if (
        this.password == '' ||
        this.yards == '' ||
        this.newPassword == '' ||
        this.submitPass == ''
      ) {
        _g.message('修改信息不能为空', 'warning')
        return false
      }
      if (this.validationPassword()) {
        _g.message('用户密码错误', 'warning')
        return false
      }
      if (this.checkSmsPassword()) {
        _g.message('短信验证码不一致', 'warning')
        return false
      }
      if (!this.checkNewPassword()) {
        _g.message('输入密码不一致请重新输入', 'warning')
        return false
      }
      return true
    },
    submit() {
      //确认修改密码
      if (!this.check()) return false
      this.updatePassword()
    },
    //验证登录用户是否存在
    validationPassword() {
      _g.apiPost('/talent/validationPassword.do', this.params).then(res => {
        if (res.status != 0) {
          return false
        } else {
          return true
        }
      })
    },
    //验证两次输入的新密码是否一致
    checkNewPassword() {
      if (this.newPassword != this.submitPass || this.submitPass == '') {
        //console.log(this.newPassword != this.submitPass)
        return false
      }
      return true
    },
    //修改用户密码方法
    updatePassword() {
      _g.apiPost('/talent/updatePassword.do', {
        newPassword: this.params.newPassword
      }).then(res => {
        _g.toMessage(res)
        this.getData()
        if (res.status == 0) {
          this.$router.push({ path: '/login' })
        }
      })
    },
    //初始化方法
    getData() {
      let that = this
      _g.apiPost('/talent/selectTalentInfo.do', this.params).then(res => {
        this.TalentInfo = res.data.resumeTalent ? res.data.resumeTalent : {}
        this.params.phone = this.TalentInfo.phone
        this.phone = _g.encryptTel(this.TalentInfo.phone)
      })
    },
    //发送手机短信验证码
    sendSmsPassword() {
      //点击发送验证码
      this.params.sms = ''

      _g.apiPost('/login/sendSmsPassword.do', {
        phone: this.TalentInfo.phone
      }).then(res => {
        _g.toMessage(res)
        if (res.status == 0) {
          this.setCodeTime()
        }
      })
    },
    //验证短信验证码是否一致
    checkSmsPassword() {
      _g.apiPost('/login/checkSMS.do', this.params).then(res => {
        //console.log(res.status)
        if (res.status != 0) {
          return false
        }
        return true
      })
    },
    init() {
      let left_time = localStorage.getItem(localSaved.passwordTime)
      if (left_time > 0 && left_time != 60) {
        this.codeLeftTime = left_time
        clearInterval(this.settime)
        this.setCodeTime()
      }
    },
    /**
     * @name 设置验证码延时
     */
    setCodeTime() {
      this.settime = setInterval(() => {
        this.codeLeftTime--
        localStorage.setItem(localSaved.passwordTime, this.codeLeftTime)
        if (this.codeLeftTime == 0) {
          clearInterval(this.settime)
          this.codeLeftTime = 60
          localStorage.setItem(localSaved.passwordTime, this.codeLeftTime)
        }
      }, 1000)
    }
  },
  created() {
    this.init()
  },
  mounted() {
    this.getData()

    // 验证码初始化
  },
  components: {}
}
</script>

<style lang="scss" scoped>
</style>
